import request from "@/utils/request";
import type {
  GetRoleListParams,
  GetRoleListApiResponse,
  GetRolePermissionListApiResponse,
} from "./model/roleModel";

const enum Api {
  getRoleListApi = "/admin/acl/role",
  addRoleApi = "/admin/acl/role/save",
  getRolePermissionListApi = "/admin/acl/permission/toAssign",
  assignRolePermissionApi = "/admin/acl/permission/doAssign",
}

/**
 * 获取角色分页列表
 * @param GetRoleListParams
 * @returns GetRoleListApiResponse
 */
export const GetRoleListApi = ({
  page,
  limit,
  roleName,
}: GetRoleListParams) => {
  return request.get<any, GetRoleListApiResponse>(
    Api.getRoleListApi + `/${page}/${limit}`,
    {
      params: {
        roleName,
      },
    }
  );
};

/**
 * 新增角色
 * @param roleName 角色名
 * @returns null
 */
export const AddRoleApi = (roleName: string) => {
  return request.post<any, null>(Api.addRoleApi, { roleName });
};

/**
 * 根据角色获取菜单
 * @param roleId 角色id
 * @returns
 */
export const GetRolePermissionListApi = (roleId: string) => {
  return request.get<any, GetRolePermissionListApiResponse>(
    Api.getRolePermissionListApi + `/${roleId}`
  );
};

/**
 * 给角色分配权限
 * @param roleId 角色id
 * @param permissionId 菜单id列表
 * @returns null
 */
export const AssignRolePermissionApi = (
  roleId: string,
  permissionId: string[]
) => {
  return request.post<any, null>(
    Api.assignRolePermissionApi,
    {},
    {
      params: {
        roleId,
        permissionId: permissionId.join(","),
      },
    }
  );
};
